// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

// import AudioManage, {soundList} from "../resources/framework/AudioManage";
// import settingMediator from "../resources/framework/settingMediator";

import AudioManage, {soundList} from "../framework/AudioManage";
import settingMediator from "../framework/settingMediator";

const {ccclass, property} = cc._decorator;

export enum switchColorList{
    red = 0,
    green = 1,
    orange = 2,
    blue = 3,
    yellow = 4,
    purple = 5
}

let bottomPicList = ["dj-k06","dj-k05","dj-k01","dj-k04","dj-k03","dj-k02"];
let handPicList = ["dj-k06b","dj-k05b","dj-k01b","dj-k04b","dj-k03b","dj-k02b"];

@ccclass
export default class switchMediator extends cc.Component {

    @property({
        type : cc.Enum(switchColorList),
        displayName : "开关颜色"
    })
    curSwitchColor : switchColorList = switchColorList.orange;

    @property(cc.Node)
    handNode : cc.Node = null;

    @property({displayName : "扳手的初始在左"})
    handDirL : boolean = false ;

    @property({type : cc.Component.EventHandler})
    toolEvent : cc.Component.EventHandler = new cc.Component.EventHandler();

    @property({type : cc.Component.EventHandler})
    toolEventMore : cc.Component.EventHandler[] = [];

    onLoad () {
        if(this.handDirL){
            this.handNode.angle = -90;
        }else{
            this.handNode.angle = 0;
        }
        this.setBoxSkin(this.node.getChildByName("hand"),handPicList[this.curSwitchColor]);
        this.setBoxSkin(this.node.getChildByName("bottom"),bottomPicList[this.curSwitchColor]);
    }

    setBoxSkin(node,url){
        cc.loader.loadRes("img/level/" + url,cc.SpriteFrame,(error, resource) => {
            if(error){
                console.log(error);
                return;
            }
            node.getComponent(cc.Sprite).spriteFrame = resource;
        })
    }

    switchAction(){
        AudioManage.playSFX(soundList.openSwitch,false);
        settingMediator.doVibration(1);
        this.node.getChildByName("bg").active = false;
        let rotation = 0;
        if (this.handDirL){
            rotation = -90;
            this.handDirL = false;
        }else{
            rotation = 90;
            this.handDirL = true;
        }
        cc.tween(this.handNode).by(0.3,{
            angle : rotation
        }).call(()=>{
            this.node.group = "default";
            cc.Component.EventHandler.emitEvents([this.toolEvent],[this.curSwitchColor]);
            cc.Component.EventHandler.emitEvents(this.toolEventMore);
        }).start();
    }

    repeatSwitchAction(){
        this.node.getChildByName("bg").active = false;
        let rotation = 0;
        if(this.handDirL){
            rotation = -90;
            this.handDirL = false;
        }else{
            rotation = 90;
            this.handDirL = true;
        }
        cc.tween(this.handNode).by(0.3,{
            angle : rotation
        }).call(()=>{
            cc.Component.EventHandler.emitEvents([this.toolEvent],[this.curSwitchColor]);
            cc.Component.EventHandler.emitEvents(this.toolEventMore);
        }).start();
    }
}
